package model

import (
	"gorm.io/gorm"
	"time"
)

type DataIndex struct {
	Id        int64  `json:"id" gorm:"column:id"`                   //
	UserId    int64  `json:"user_id" gorm:"column:user_id"`         // 用户id
	FtpInfoId int64  `json:"ftp_info_id" gorm:"column:ftp_info_id"` // 数据所属服务器id
	DataType  string `json:"data_type" gorm:"column:data_type"`     // 数据类型
	FileTime  int64  `json:"file_time" gorm:"column:file_time"`     // 文件时间
	FileRow   int64  `json:"file_row" gorm:"column:file_row"`       // 文件行数
	UpdatedAt int64  `json:"updated_at" gorm:"column:updated_at"`   // 修改时间
	CreatedAt int64  `json:"created_at" gorm:"column:created_at"`   // 添加时间
}

func (DataIndex) TableName() string {
	return "sm_data_index"
}

func (u DataIndex) BeforeUpdate(tx *gorm.DB) (err error) {
	now := time.Now()
	unixTime := now.Unix()
	u.UpdatedAt = unixTime
	u.CreatedAt = unixTime
	return nil
}

type DataIndexM struct {
	DB *gorm.DB
}

func NewDataIndexM() *DataIndexM {
	return &DataIndexM{
		GetDB(),
	}
}

// GetUserDataIndex 获取用户数据索引
func (s *DataIndexM) GetUserDataIndex(userId int64, ftpInfoId int64) []DataIndex {
	var dataIndex []DataIndex
	result := s.DB.Where("user_id =? and ftp_info_id =?", userId, ftpInfoId).Find(&dataIndex)
	/*sqlString := result.Statement.SQL.String()
	fmt.Println("生成的 SQL 语句:", sqlString)*/
	if result.Error != nil {
		panic(result.Error)
	}
	return dataIndex
}

// SaveUserDataIndex 保存用户数据索引
func (s *DataIndexM) SaveUserDataIndex(userId int64, FtpInfoId int64, DataType string, fileTime int64, fileRow int64, insert bool) bool {

	if insert {
		result := s.DB.Omit("id").Create(&DataIndex{UserId: userId, FtpInfoId: FtpInfoId, DataType: DataType, FileTime: fileTime, FileRow: fileRow})
		if result.Error != nil {
			panic(result.Error)
		}
	} else {

		result := s.DB.Model(DataIndex{}).Where("user_id =? and ftp_info_id =? and data_type =?", userId, FtpInfoId, DataType).Updates(&DataIndex{FileTime: fileTime, FileRow: fileRow})
		//sqlString := result.Statement.SQL.String()
		//fmt.Println("生成的 SQL 语句:", sqlString)
		if result.Error != nil {
			panic(result.Error)
		}
	}

	return true
}

// SaveUserDataIndex 保存用户数据索引 (特殊类型)
func (s *DataIndexM) SaveUserUniqueDataIndex(userId int64, FtpInfoId int64, DataType string, fileTime int64, fileRow int64, insert bool) bool {
	var count int64
	result := s.DB.Model(&DataIndex{}).Where("user_id =? and ftp_info_id =? and data_type =?", userId, FtpInfoId, DataType).Count(&count)
	if result.Error != nil {
		panic(result.Error)
	}
	//fmt.Println("数量为:", count)
	if count < 1 {
		result := s.DB.Omit("id").Create(&DataIndex{UserId: userId, FtpInfoId: FtpInfoId, DataType: DataType, FileTime: fileTime, FileRow: fileRow})
		if result.Error != nil {
			panic(result.Error)
		}
	} else {
		result := s.DB.Model(DataIndex{}).Where("user_id =? and ftp_info_id =? and data_type =?", userId, FtpInfoId, DataType).Updates(&DataIndex{FileTime: fileTime, FileRow: fileRow})
		//sqlString := result.Statement.SQL.String()
		//fmt.Println("生成的 SQL 语句:", sqlString)
		if result.Error != nil {
			panic(result.Error)
		}
	}

	return true
}
